/*
 * @Description: 新建事项中打印映射
 */
import sendCommon from '@/handles/common/send';
import {createPrintFragment, doPrint} from '@/handles/common/print';
import globalMethods from '@/plugins/methods';
import globalData, {BodyTypeEnums} from "@/plugins/data";
import {viewOffice} from "@/plugins/office";
import {checkOFDComplete} from "@/plugins/office/extend/ofd-office";



function newDoPrint(resolve) {
    let selectValue = 'colPrint';
    const ctx = window._getZWIframe();
    let {bodyType, forwardMember} = sendCommon.useBaseInfo.baseInfo.businessData.summary;
    const printFrom = globalData.method;
    if (forwardMember) {
        bodyType = globalData.bodyType;
    }
    switch (bodyType) {
        case '10': //isHTML
            doPrint(createPrintFragment(printFrom), '', printFrom);
            resolve();
            break;
        case '20': //isForm
            var isOffice = $('#zwOfficeIframe').css('display');
            var hasFocus = $('#zwOfficeIframe').attr('hasFocus');
            if (isOffice == 'block' && hasFocus == 'true') {
                globalMethods.confirm({
                    title: globalMethods.i18n('collaboration.print.SelectType.label'),
                    // h 参数需保留，渲染需要该参数
                    // eslint-disable-next-line no-unused-vars
                    content: (h) => <a-radio-group
                      defaultValue={selectValue}
                      onChange={({target}) => selectValue = target.value}>
                        <a-radio style="height:30px;line-height:30px;display:block" value='mainpp'> {globalMethods.i18n('collaboration.print.type.form')}</a-radio>
                        <a-radio style="height:30px;line-height:30px;display:block"  value='colPrint'> {globalMethods.i18n('collaboration.print.type.coll')}</a-radio>
                    </a-radio-group>,
                    onOk:()=> {
                        if (selectValue === 'mainpp') {
                            if (printFrom == 'newColl') {
                                window.frames['zwOfficeIframe'].window.OfficeAPI.officePrint();
                            } else {
                                window.frames['zwOfficeIframe'].window.OfficeAPI.officePrint();
                            }
                        } else {
                            //打印意见
                            doPrint(createPrintFragment(printFrom), 'colPrint', printFrom);
                        }
                    }
                });
                $('#zwIframe').css('visibility', 'visible');
                resolve();
            } else {
                doPrint(createPrintFragment(printFrom), '', printFrom);
                resolve();
            }
            break;
        default:
            //判断是否存在在线转换，如果存在 则采用web直接打印
            const officeTransIframeDom = ctx.document.getElementById('officeTransIframe');
            if (officeTransIframeDom && !window.isWpsTransTypeV2()) {
                console.log('print', officeTransIframeDom);
                const xmlns = officeTransIframeDom.contentWindow.document
                  ?.getElementById('htmlFrame')
                  ?.contentWindow.document.querySelector('html')
                  ?.getAttribute('xmlns:x');
                if (!xmlns || !xmlns.includes('urn:schemas-microsoft-com:office:excel')) {
                    doPrint(createPrintFragment(printFrom), 'onLineTrans', 'onLineTrans');
                    return;
                }
            }


            globalMethods.confirm({
                title: globalMethods.i18n('collaboration.print.SelectType.label'),
                // h 参数需保留，渲染需要该参数
                // eslint-disable-next-line no-unused-vars
                content: (h) => <a-radio-group
                  defaultValue={selectValue}
                  onChange={({target}) => selectValue = target.value}>
                    <a-radio style="height:30px;line-height:30px;display:block" value='mainpp'> {globalMethods.i18n('collaboration.print.type.office')}</a-radio>
                    <a-radio style="height:30px;line-height:30px;display:block"  value='colPrint'> {globalMethods.i18n('collaboration.print.type.coll')}</a-radio>
                </a-radio-group>,
                onOk:async ()=> {
                    console.log('打印入口',selectValue,officeTransIframeDom);
                    if (selectValue === 'mainpp') {
                        //检查是否是转换的
                        if (officeTransIframeDom){
                            await viewOffice({zwIframe: ctx, bodyType});
                        }
                        if (bodyType===BodyTypeEnums.ofd.toString()){
                            //V5-6411 新建协同，正文选择ofd后 点击打印正文。没有效果
                            if (await checkOFDComplete({zwIframe: ctx})) {
                                ctx.OfdAPI.printOfd();
                            }
                            console.log('ofd print');
                            return;
                        }
                        //pdf的打印
                        if (bodyType===BodyTypeEnums.pdf.toString()){
                            console.log('pdf print ',ctx.PdfAPI)
                            //新建
                            if (!globalData.isSummary){
                                await ctx.PdfAPI.pdfFullSize(false,()=>{
                                    ctx.PdfAPI.pdfPrint();
                                })
                            }
                            //处理页面
                            //let useWeboffice = ctx.PdfAPI.canUseWebOfficeForPdfAync({webRoot:ctx.PdfUtils.getWebRoot()});
                            //officeTransIframeDom TODO office转换逻辑？？
                            let v3xTmp = window.getA8Top().v3x || ctx.v3x;
                            let isOfdPrint=v3xTmp.needOpenPdfByOfd();
                            if (!isOfdPrint){
                                await ctx.PdfAPI.checkPDFOpenState(null,()=>{
                                    
                                });
                                await ctx.PdfAPI.pdfFullSize();
                                await ctx.PdfAPI.pdfPrint();
                            }
                            return;
                        }
                        if (officeTransIframeDom){
                            //直接打印，office转换的
                            await ctx.OfficeAPI.officePrint();
                            return;
                        }
                        doPrint(Promise.resolve({}), 'mainpp', printFrom);
                    } else {
                        doPrint(createPrintFragment(printFrom), 'colPrint', printFrom);
                    }
                },
            });
            resolve();
            
    }
}

export default () =>
    new Promise((resolve) => {
        newDoPrint(resolve);
    });
